package com.wantong.jianpu.dialogs;

import android.content.Context;
import android.view.View;

import androidx.annotation.NonNull;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.view.TimePickerView;
import com.lxj.xpopup.core.CenterPopupView;
import com.wantong.jianpu.R;
import com.wantong.jianpu.customviews.CommSelectView;

import java.util.Calendar;

import me.goldze.mvvmhabit.utils.DateUtil;

/**
 * @author PACY
 * @date 2022/5/7
 * description:
 */
public class TourSignPopupView extends CenterPopupView {
    TimePickerView popup;
    CommSelectView selectTimeCsv;
    public TourSignPopupView(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.toursign_popup_view;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        selectTimeCsv=findViewById(R.id.selectTime_csv);
        selectTimeCsv.setContent(DateUtil.getNowDate(DateUtil.DatePattern.ONLY_MINUTE));
        findViewById(R.id.close_tv).setOnClickListener((v) -> {
            dismiss();
        });
        selectTimeCsv.setOnClickListener(v -> {
            showDatePicker();
        });
    }

    /**
     * 日期选择器
     */
    private void showDatePicker() {
        Calendar cal = Calendar.getInstance();
        Calendar cal2 = Calendar.getInstance();
        cal.set(cal2.get(Calendar.YEAR) - 5, 0, 1);

        if (popup == null) {
            popup = new TimePickerBuilder(getContext(), (date, v) -> {
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(date);
                String month = (calendar.get(Calendar.MONTH) + 1) + "";
                month = month.length() > 1 ? month : "0" + month;
                selectTimeCsv.setContent(calendar.get(Calendar.YEAR) + "-" + month+"-"+calendar.get(Calendar.DAY_OF_MONTH)+" "+calendar.get(Calendar.HOUR_OF_DAY)+":"+calendar.get(Calendar.MINUTE));
            })
                    .setType(new boolean[]{true, true, true, true, true, false})
                    .setTitleText("选择时间")
                    .setRangDate(cal, cal2)
                    .build();
        }
        popup.setDate(Calendar.getInstance());
        popup.show();

    }
}
